/*
 *  PCX.h
 *  SCMS
 *
 *  Created by poiuy_qwert on 01/03/13.
 *
 */

#pragma once

#include "Exception.h"
#include "Pack.h"

class Palette;

#pragma pack(1)
struct PCX_HEADER {
	char sign[4];
	unsigned short xMin;
	unsigned short yMin;
	unsigned short xMax;
	unsigned short yMax;
	unsigned short hdpi;
	unsigned short vdpi;
	char unused1[49];
	unsigned char planes;
	unsigned short bytesperline;
	unsigned short palinfo;
	unsigned short hscreensize;
	unsigned short vscreensize;
	char unused2[54];
};
#pragma pack(0)

class PCX {
	Palette *palette;
	unsigned char* planes;
	int width, height;
	bool specialPalette;
public:
	PCX()
		: palette(NULL),planes(NULL),width(0),height(0) {}
	PCX(const char *filename, bool specialPalette = false)
		{ this->open_file(filename, specialPalette); }
	PCX(const char *buffer, int size, bool specialPalette = false)
		{ this->open_data(buffer, size, specialPalette); }
	PCX(const unsigned char *plane, int width, int height, Palette *palette)
		: palette(palette)
		{ this->set_plane(plane, width, height); }
	~PCX()
		{ this->close(); }

	void open_file(const char *filename, bool specialPalette = false);
	void open_data(const char *buffer, int size, bool specialPalette = false);
	void save_file(const char *filename);
	char* save_data(int &size);
	
	int get_width()
		{ return this->width; }
	int get_height()
		{ return this->height; }
	unsigned char* get_planes()
		{ return this->planes; }
	void set_plane(const unsigned char *plane, int width, int height);
	Palette* get_palette()
		{ return this->palette; }
	void set_palette(Palette *palette)
		{ this->palette = palette; }
	void close()
		{ if (this->planes != NULL) delete [] this->planes; }

	Palette *get_palette(unsigned int specialPaletteRow);
};